define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: 'house/history/index',
          add_url: 'house/history/add',
          edit_url: 'house/history/edit',
          del_url: 'house/history/del',
          multi_url: 'house/history/multi',
          table: 'house_history',
        }
      })

      var table = $("#table")

      // 初始化表格
      table.bootstrapTable({
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: 'id',
        sortName: 'id',
        columns: [
          [
            {checkbox: true},
            {field: 'id', title: __('Id')},
            {field: 'name', title: __('Name')},
            {field: 'identity_id', title: __('IdentityId')},
            {field: 'tel', title: __('Tel')},
            {field: 'occupation', title: __('Occupation')},
            {field: 'birth', title: __('Birth')},
            {
              field: 'gender', title: __('Gender'), operate: false, formatter: function (value) {
                var genders = [__('Women'), __('Man')]
                return genders[value]
              }
            },
            {
              field: 'owner_type', title: __('OwnerType'), operate: false, formatter: function (value) {
                var ownerTypes = ['', __('Owner'), __('Member'), __('Lessee')]
                return ownerTypes[value]
              }
            },
            {
              field: 'community', title: __('Community'), formatter: function (val) {
                return val.name
              }
            },
            {
              field: 'house', title: __('House'), formatter: function (val) {
                return val.name
              }
            },
            {
              field: 'type', title: __('MoveType'), operate: false, formatter: function (value) {
                var statuses = [__('MoveIn'), __('MoveOut')]
                return statuses[value]
              }
            },
            {field: 'create_time', title: __('CreateTime'), formatter: Table.api.formatter.datetime},
            {field: 'update_time', title: __('UpdateTime'), formatter: Table.api.formatter.datetime},
            {field: 'remark', title: __('Remark')},
            {
              field: 'operate',
              title: __('Operate'),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate
            }
          ]
        ]
      })
      // 为表格绑定事件
      Table.api.bindevent(table)
    },
    add: function () {
      Controller.api.handleCommunityState()
      Controller.api.bindevent()
    },
    edit: function () {
      Controller.api.handleCommunityState()
      Controller.api.bindevent()
    },
    api: {
      bindevent: function () {
        Form.api.bindevent($("form[role=form]"))
      },
      handleCommunityState: function (showAll) {
        Controller.api.initCommunityState(showAll)
        $("#community_code").bind("change", function () {
          Controller.api.initCommunityState(showAll)
        })
      },
      initCommunityState: function (showAll) {
        var house_code = $("#house_code").val()
        var community_code = $('#community_code').val()
        $.ajax({
          type: "POST",
          url: 'owners/index/get_house_by_cm_code',
          async: true,
          cache: false,
          dataType: "json",
          data: {community_code: community_code},
          success: function (data) {
            var house = data.house

            var houseHtml = showAll ? '<option value="">全部</option>' : ''
            $.each(house, function (index, item) {
              houseHtml += '<option value="' + item.code + '">' + item.name + '</option>'
            })
            if (houseHtml == '') {
              houseHtml = '<option value="">没有任何选中项</option>'
            }
            $("#house_code").html(houseHtml)
            $("#house_code").selectpicker("refresh")
          }
        })
      }
    }
  }
  return Controller
})